package com.longjianghuasheng.logistics.business.detail

import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_BONDED
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_REPORT_FOR_INSPECTION
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_SITE_LOADING_AND_UNLOADING
import com.longjianghuasheng.logistics.business.freight.ServiceListFragment.Companion.TYPE_WAREHOUSING_AND_DISTRIBUTION
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.entity.StorageInfoResponse
import com.longjianghuasheng.logistics.ext.commit2
import com.longjianghuasheng.logistics.ext.toastNormal
import com.longjianghuasheng.logistics.network.apiService

class DetailViewModel : BaseMultiStateViewModel() {

    var isTheSlingLoadingAndUnloading = false  // 是否是吊具装卸

    var mobile = ""

    var address = ""

    var type = 0

    var id = 0

    var names = ""

    var rules2 = ""

    var rules = ""

    var nums2 = ""

    var nums = ""

    var start_address = ""

    var end_address = ""

    var packets = ""

    var types = ""

    var imorex = ""

    val result by lazy {
        MutableLiveData<StorageInfoResponse>()
    }

    val commitResult by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    fun form_info() {
        getData2({ apiService.form_info(id) }, result)

    }


    fun service_form() {


//        when (type) {
//             -> {}
//            else -> {}
//        }


        if (!check()) return


        val map = hashMapOf<String, String>().apply {

            put("service_id", id.toString())

            if (isTheSlingLoadingAndUnloading) {
                put("rules", rules2)
                put("nums", nums2)
                return@apply
            }
            if (type == TYPE_SITE_LOADING_AND_UNLOADING) {
                put("rules2", rules2)
                put("nums2", nums2)
                put("packets", packets)
                put("types", types)

            }

            if (rules.isNotBlank()) put("rules", rules)
            put("nums", nums)

            when (type) {
                TYPE_SITE_LOADING_AND_UNLOADING,
                TYPE_WAREHOUSING_AND_DISTRIBUTION,
                TYPE_BONDED,
                TYPE_REPORT_FOR_INSPECTION -> {
                    if (start_address.isNotBlank()) put("start_address", start_address)
                    if (end_address.isNotBlank()) put("end_address", end_address)

                    if (type != TYPE_WAREHOUSING_AND_DISTRIBUTION) put("names", names)
                    put("packets", packets)
                    put("types", types)

                    if (imorex.isNotBlank()) {

                        put("imorex", imorex)
                    }

                }
            }


        }


        commit2({ apiService.service_form(map) }, commitResult)
    }

    private fun check(): Boolean {


        if (isTheSlingLoadingAndUnloading) {

            if (rules2.isBlank()) {
                toastNormal("请填写集装箱规格")
                return false
            }
            if (nums2.isBlank()) {
                toastNormal("请填写集装箱数量")
                return false
            }

            return true
        }


        if (type != TYPE_WAREHOUSING_AND_DISTRIBUTION && names.isBlank()) {
            toastNormal("请填写货物名称")
            return false
        }

        if (type == TYPE_WAREHOUSING_AND_DISTRIBUTION) {

            if (start_address.isBlank()) {
                toastNormal("请填写起始地")
                return false
            }
            if (end_address.isBlank()) {
                toastNormal("请填写目的地")
                return false
            }

        }
        if (rules.isBlank()) {
            toastNormal("请填写规格")
            return false
        }
        if (nums.isBlank()) {
            toastNormal("请填写数量")
            return false
        }

        if (packets.isBlank()) {
            toastNormal("请填写包装")
            return false
        }
        if (types.isBlank()) {
            toastNormal("请填写种类")
            return false
        }

        if (type == TYPE_SITE_LOADING_AND_UNLOADING) {

            if (rules2.isBlank()) {
                toastNormal("请填写集装箱规格")
                return false
            }
            if (nums2.isBlank()) {
                toastNormal("请填写集装箱数量")
                return false
            }

        }


        return true
    }
}